package com.example.sinya.exoplayer.demo;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

/**
 * @author Sinya
 * @editor
 * @date 16/4/2018 3:18 PM
 * @describe
 */
public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {

        final Integer[] rawIdArray = {//
                R.raw.heinrich_digital_demo_1, //
                R.raw.heinrich_digital_demo_2, //
                R.raw.heinrich_digital_demo_3, //
                R.raw.heinrich_digital_demo_5, //
                R.raw.wx_camera_0,//
                R.raw.wx_camera_1,//
                R.raw.wx_camera_2,//
                R.raw.wx_camera_3,//
        };
        setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, rawIdArray));

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                openVideoByExoPlayer(rawIdArray[position]);
            }
        });
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                openVideoByMediaPlayer(rawIdArray[position]);
                return true;
            }
        });
    }

    private void openVideoByExoPlayer(int rawId) {
        if (rawId > 0) {
            Intent intent = new Intent();
            intent.putExtra(BaseActivity.RAW_ID, rawId);
            intent.setClass(MainActivity.this, ExoPlayerActivity.class);
            startActivity(intent);
        }
    }

    private void openVideoByMediaPlayer(int rawId) {
        if (rawId > 0) {
            Intent intent = new Intent();
            intent.putExtra(BaseActivity.RAW_ID, rawId);
            intent.setClass(MainActivity.this, MediaPlayerActivity.class);
            startActivity(intent);
        }
    }
}